/**
 * com.chaeron.GPS - An API for access to GPS devices.
 *
 *
 * Copyright (c) 2001, 2002 Chaeron Corporation,
 * All rights reserved.
 * 
 * Chaeron grants you a non-exclusive license to use, modify and re-distribute
 * this program under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version, provided that this copyright notice and 
 * license appear on all copies of the software.
 * 
 * Software is provided "AS IS," without a warranty of any kind. ALL EXPRESS OR
 * IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. THE ENTIRE RISK ARISING OUT OF USING THE SOFTWARE IS ASSUMED
 * BY THE LICENSEE. See the GNU General Public License for more details.
 *
 * CHAERON AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR DISTRIBUTING SOFTWARE.
 * IN NO EVENT WILL CHAERON OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
 * PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL
 * OR PUNITIVE DAMAGES, HOWEVER CAUSED AN REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF CHAERON HAS
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * @version     1.02
 * @author  	Andrzej Jan Taramina, Chaeron Corporation
 *  			andrzej@chaeron.com
 */

package com.chaeron.platform.common;

import java.io.*;

import com.chaeron.platform.common.*;
import javax.comm.*;

public class SerialPort extends SerialPortBase implements Stream
{
	public static int		DEFAULT_READ_TIMEOUT   		   		= 0;
	
	javax.comm.SerialPort 	sPort;
	InputStream 			is;
	OutputStream 			os;
	boolean 				isOpen		= false;
	
	public SerialPort( int number, int baudRate )
	{
		try {
			CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier( "COM" + ( number + 1 ) );
			sPort = (javax.comm.SerialPort)portId.open( "GPS", 1000 );
			sPort.setSerialPortParams( baudRate, 8, 1, 0 );
			is = sPort.getInputStream();
			os = sPort.getOutputStream();
			isOpen = true;
		}
		catch( Exception e ) {
			System.out.println( "Open Error: " + e );
		}
	}
	
	public boolean setFlowControl( boolean on )
	{
		boolean ret = false;
		
		try {
			if( on ) {
				sPort.setFlowControlMode( javax.comm.SerialPort.FLOWCONTROL_RTSCTS_IN & javax.comm.SerialPort.FLOWCONTROL_RTSCTS_OUT );
			} else {
				sPort.setFlowControlMode( javax.comm.SerialPort.FLOWCONTROL_NONE );
			}
			ret = true;
		}
		catch( UnsupportedCommOperationException e ) {
			
		}
		
		return( ret );
	}
	
	public boolean setReadTimeout( int millis )
	{
		boolean	ret		= false;
		
		try {
			sPort.enableReceiveTimeout( millis );
			ret = true;
		}
		catch( UnsupportedCommOperationException e ) {
		}
		
		return( ret );
	}
	
	public void flush()
	{
		try {
			os.flush();
		}
		catch( IOException e ) {
		}
	}
	
	public void close()
	{
		boolean	ret		= false;
		
		if( isOpen ) {
			sPort.close();
			isOpen = false;				// AJT - Bug fix 01/05/2002
			ret = true;
		}
		
		// return( ret );
	}
	
	public boolean isOpen()
	{
		return( isOpen );
	}
	
	public int readCheck()
	{
		int	available	= -1;		// AJT - Enhancement 01/05/2002
		
		if( isOpen ) {
			try {
				available = is.available();
			}
			catch( Exception e ) {
				System.out.println( "ReadCheck error! " + e );
			}
		} 
		
		return( available );   
	}
	
	public int readBytes( byte buf[], int start, int count )
	{
		int	bytes		= -1;
		
		if( isOpen ) {
			try {
				bytes = is.read( buf, start, count );
			}
			catch( Exception e ) {
				System.out.println( "Read error! " + e );
			}
		}
		
		return( bytes );
	}
	
	public int writeBytes( byte buf[], int start, int count )
	{
		int	bytes		= -1;
		
		if( isOpen ) {
			try {
				os.write( buf, start, count );
				bytes = count; 
			}
			catch( Exception e ) {
				System.out.println( "Write error! " + e );
			}
		}
		
		return( bytes );
	}
	
}
